﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Odbc;

namespace caculate
{
    public partial class NewParent : Form
    {
        public NewParent()
        {
            InitializeComponent();
        }

        public class PublicValue1
        {
            public static string prtaccount;
            public static string prtpassword;
            public static string prtname;
            public static string chindrenname;
        }
           

        private void CreateNewUser_Click(object sender, EventArgs e)
        {
            string conString = "DSN=t2;UID=root;PWD=123456"; //ODBC的数据源名
            OdbcConnection con1 = new OdbcConnection(conString);
            OdbcConnection con2 = new OdbcConnection(conString);
            OdbcConnection con3 = new OdbcConnection(conString);
            con1.Open();
            con2.Open();
            con3.Open();


                string sql1 = "select * from TeacherMessage where TeacherAccount='" + PublicValue1.prtaccount + "'";
                OdbcCommand com1 = new OdbcCommand(sql1, con1);
                OdbcDataReader sdr1 = com1.ExecuteReader();//判断表中是否有相同账户名
            if (sdr1.Read())
            {
                
                AccountText.Text = "";
                PasswordText.Text = "";
                ParentNameText.Text = "";
                ChindrenNameText.Text = "";
                MessageBox.Show("用户名重复，请重新输入!", "提示");
            }
            else
            {
                string sql2 = "insert into TeacherMessage values('"+PublicValue1.prtaccount+"','" + PublicValue1.prtpassword + "','" + PublicValue1.prtname + "','" + PublicValue1.chindrenname + "')";
                OdbcCommand com2 = new OdbcCommand(sql2, con2);//注册新用户
                int i1 = Convert.ToInt32(com2.ExecuteNonQuery());//执行语句
               // MessageBox.Show(String.Format("{0}", i1));

                string sql3 = "select TeacherAccount from TeacherMessage where TeacherAccount='" + PublicValue1.prtaccount + "'";
                OdbcCommand com3 = new OdbcCommand(sql3, con3);
                OdbcDataReader sdr2 = com3.ExecuteReader();//判断表中是否成功添加新用户
                if(sdr2.Read())
                {
                    AccountText.Text = "";
                    PasswordText.Text = "";
                    ParentNameText.Text = "";
                    ChindrenNameText.Text = "";
                    MessageBox.Show("已成功注册！","提示");
                }
                else
                {
                   MessageBox.Show("注册失败！", "提示");
                }
                //sdr2.Close();
            }
            //sdr1.Close();

        }

        private void AccountText_TextChanged(object sender, EventArgs e)
        {
            PublicValue1.prtaccount = AccountText.Text;
        }

        private void PasswordText_TextChanged(object sender, EventArgs e)
        {
            PublicValue1.prtpassword = PasswordText.Text;

        }

        private void ParentNameText_TextChanged(object sender, EventArgs e)
        {
            PublicValue1.prtname = ParentNameText.Text;
        }

        private void ChindrenNameText_TextChanged(object sender, EventArgs e)
        {
            PublicValue1.chindrenname = ChindrenNameText.Text;
        }
    }
}
